package com.guodongbaohe.app.activity;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.guodongbaohe.app.R;
import com.guodongbaohe.app.base_activity.BaseActivity;
import com.guodongbaohe.app.bean.GaoYongJinBean;
import com.guodongbaohe.app.bean.ShopBasicBean;
import com.guodongbaohe.app.common_constant.Constant;
import com.guodongbaohe.app.common_constant.MyApplication;
import com.guodongbaohe.app.dialogfragment.BaseNiceDialog;
import com.guodongbaohe.app.dialogfragment.NiceDialog;
import com.guodongbaohe.app.dialogfragment.ViewConvertListener;
import com.guodongbaohe.app.dialogfragment.ViewHolder;
import com.guodongbaohe.app.myokhttputils.response.JsonResponseHandler;
import com.guodongbaohe.app.port.DemoTradeCallback;
import com.guodongbaohe.app.util.ClipContentUtil;
import com.guodongbaohe.app.util.CommonUtil;
import com.guodongbaohe.app.util.EncryptUtil;
import com.guodongbaohe.app.util.GsonUtil;
import com.guodongbaohe.app.util.JumpToShopDetailUtil;
import com.guodongbaohe.app.util.ParamUtil;
import com.guodongbaohe.app.util.PreferUtils;
import com.guodongbaohe.app.util.ToastUtils;
import com.guodongbaohe.app.util.VersionUtil;
import com.guodongbaohe.app.util.WebViewUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class BaseH5Activity extends BaseActivity {
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.webview)
    WebView webview;
    String url;
    ImageView iv_back;
    ImageView iv_right;
    private AlibcShowParams alibcShowParams;//页面打开方式，默认，H5，Native
    private String share_url, share_title, share_content, share_img;

    @Override
    public int getContainerView() {
        return R.layout.baseh5activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        ButterKnife.bind( this );
        iv_back = findViewById( R.id.iv_back );
        iv_right = findViewById( R.id.iv_right );
        setRightIVVisible();
        iv_right.setImageResource( R.mipmap.webview_reload );
        Intent intent = getIntent();
        url = intent.getStringExtra( "url" );
        alibcShowParams = new AlibcShowParams( OpenType.Native, true );
        WebSettings settings = webview.getSettings();
        webview.setVerticalScrollBarEnabled( false );
        settings.setJavaScriptEnabled( true );
        settings.setSupportZoom( false );
        settings.setBuiltInZoomControls( true );
        webview.setWebViewClient( new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webview.loadUrl( url, WebViewUtil.getWebViewHead( getApplicationContext() ) );
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading( view, request );
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        } );

        webview.setWebChromeClient( new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progressBar.setVisibility( View.GONE );
                } else {
                    progressBar.setVisibility( View.VISIBLE );
                    progressBar.setProgress( newProgress );
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                setMiddleTitle( title );
                super.onReceivedTitle( view, title );
            }

        } );
        iv_back.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webview.canGoBack()) {
                    webview.goBack();
                } else {
                    finish();
                }
            }
        } );
        webview.loadUrl( url, WebViewUtil.getWebViewHead( getApplicationContext() ) );
        webview.addJavascriptInterface( new DemoJavascriptInterface(), "daihao" );
        initRightListener();
    }

    /*webview刷新*/
    private void initRightListener() {
        iv_right.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webview != null) {
                    webview.reload();
                }
            }
        } );
    }

    public class DemoJavascriptInterface {

        /*地推物料交互到淘宝详情（不用转高拥接口）*/
        @JavascriptInterface
        public void shareIndex(String id) {
            Log.i( "商品id", id );
            if (PreferUtils.getBoolean( getApplicationContext(), "isLogin" )) {
                AlibcBasePage page = new AlibcDetailPage( id );
                HashMap<String, String> exParams = new HashMap<>();
                exParams.put( "isv_code", "appisvcode" );
                exParams.put( "alibaba", "阿里巴巴" );
                AlibcTrade.show( BaseH5Activity.this, page, alibcShowParams, null, exParams, new DemoTradeCallback() );
                /*清空剪切板内容*/
                ClipboardManager cm = (ClipboardManager) getSystemService( Context.CLIPBOARD_SERVICE );
                if (cm.hasPrimaryClip()) {
                    cm.setPrimaryClip( ClipData.newPlainText( null, "" ) );
                }
            } else {
                Intent intent = new Intent( getApplicationContext(), LoginAndRegisterActivity.class );
                startActivity( intent );
            }
        }

        /*地推物料复制按钮交互*/
        @JavascriptInterface
        public void copy(String url) {
            Log.i( "看看地址", url + " 值 " );
            if (!TextUtils.isEmpty( url )) {
                ClipContentUtil.getInstance( getApplicationContext() ).putNewSearch( url );//保存记录到数据库
                ClipboardManager systemService = (ClipboardManager) getSystemService( Context.CLIPBOARD_SERVICE );
                systemService.setPrimaryClip( ClipData.newPlainText( "text", url ) );
            }
        }

        /*到本地商品详情交互*/
        @JavascriptInterface
        public void toShopDetail(String shop_id) {
            Log.i( "商品详情id", shop_id );
            getShopBasicData( shop_id );
        }

        /*转高佣接口  到淘宝详情界面*/
        @JavascriptInterface
        public void highCommissionToTaoBaoDetail(String shop_id) {
            Log.i( "商品详情高拥id", shop_id );
            if (PreferUtils.getBoolean( getApplicationContext(), "isLogin" )) {
                getGaoYongJinData( shop_id );
            } else {
                Intent intent = new Intent( getApplicationContext(), LoginAndRegisterActivity.class );
                startActivity( intent );
            }
        }

        /*分享*/
        @JavascriptInterface
        public void invitation(String url, String title, String content, String img) {
            Log.i( "分享内容", url + "  " + title + "   " + content + "   " + img );
            share_url = url;
            share_title = title;
            share_content = content;
            share_img = img;
            /*自定义九宫格样式*/
            customShareStyle();
        }

        /*图片长按保存*/
        @JavascriptInterface
        public void preserve(String img) {
            saveImgToLocal( img );
        }

        /*淘宝会场*/
        @JavascriptInterface
        public void goShopping(String myUrl) {
            runOnUiThread( new Runnable() {
                @Override
                public void run() {
                    AlibcBasePage page = new AlibcPage( myUrl );
                    HashMap<String, String> exParams = new HashMap<>();
                    exParams.put( "isv_code", "appisvcode" );
                    exParams.put( "alibaba", "阿里巴巴" );
                    AlibcTrade.show( BaseH5Activity.this, page, alibcShowParams, null, exParams, new DemoTradeCallback() );
                }
            } );
        }

    }

    /*跳转淘宝链接*/
    String coupon_url;

    private void getGaoYongJinData(String goods_id) {
        long timelineStr = System.currentTimeMillis() / 1000;
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put( Constant.TIMELINE, String.valueOf( timelineStr ) );
        map.put( Constant.PLATFORM, Constant.ANDROID );
        map.put( "member_id", PreferUtils.getString( getApplicationContext(), "member_id" ) );
        map.put( "goods_id", goods_id );
        map.put( "coupon_id", "" );
        map.put( Constant.SHOP_REFERER, "" );
        map.put( Constant.GAOYONGJIN_SOURCE, "" );
        String qianMingMapParam = ParamUtil.getQianMingMapParam( map );
        String token = EncryptUtil.encrypt( qianMingMapParam + Constant.NETKEY );
        map.put( Constant.TOKEN, token );
        String param = ParamUtil.getMapParam( map );
        Log.i( "高佣金返回值参数", Constant.BASE_URL + Constant.GAOYONGIN + "?" + param );
        MyApplication.getInstance().getMyOkHttp().post()
                .url( Constant.BASE_URL + Constant.GAOYONGIN + "?" + param )
                .addHeader( "x-userid", PreferUtils.getString( getApplicationContext(), "member_id" ) )
                .addHeader( "x-appid", Constant.APPID )
                .addHeader( "x-devid", PreferUtils.getString( getApplicationContext(), Constant.PESUDOUNIQUEID ) )
                .addHeader( "x-nettype", PreferUtils.getString( getApplicationContext(), Constant.NETWORKTYPE ) )
                .addHeader( "x-agent", VersionUtil.getVersionCode( getApplicationContext() ) )
                .addHeader( "x-platform", Constant.ANDROID )
                .addHeader( "x-devtype", Constant.IMEI )
                .addHeader( "x-token", ParamUtil.GroupMap( getApplicationContext(), PreferUtils.getString( getApplicationContext(), "member_id" ) ) )
                .enqueue( new JsonResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        super.onSuccess( statusCode, response );
                        Log.i( "高佣金返回值", response.toString() );
                        try {
                            JSONObject jsonObject = new JSONObject( response.toString() );
                            if (jsonObject.getInt( "status" ) >= 0) {
                                GaoYongJinBean bean = GsonUtil.GsonToBean( response.toString(), GaoYongJinBean.class );
                                if (bean == null) return;
                                String coupon_remain_count = bean.getResult().getCoupon_remain_count();
                                if (!TextUtils.isEmpty( coupon_remain_count ) && Double.valueOf( coupon_remain_count ) > 0) {
                                    coupon_url = bean.getResult().getCoupon_click_url();
                                } else {
                                    coupon_url = bean.getResult().getItem_url();
                                }
                                AlibcBasePage page = new AlibcPage( coupon_url );
                                HashMap<String, String> exParams = new HashMap<>();
                                exParams.put( "isv_code", "appisvcode" );
                                exParams.put( "alibaba", "阿里巴巴" );
                                AlibcTrade.show( BaseH5Activity.this, page, alibcShowParams, null, exParams, new DemoTradeCallback() );
                            } else {
                                String result = jsonObject.getString( "result" );
                                ToastUtils.showToast( getApplicationContext(), result );
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        ToastUtils.showToast( getApplicationContext(), Constant.NONET );
                    }
                } );
    }

    /*商品详情头部信息*/
    private void getShopBasicData(String shopId) {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put( "goods_id", shopId );
        String param = ParamUtil.getMapParam( map );
        MyApplication.getInstance().getMyOkHttp().post().url( Constant.BASE_URL + Constant.SHOP_HEAD_BASIC + "?" + param )
                .tag( this )
                .addHeader( "x-appid", Constant.APPID )
                .addHeader( "x-devid", PreferUtils.getString( getApplicationContext(), Constant.PESUDOUNIQUEID ) )
                .addHeader( "x-nettype", PreferUtils.getString( getApplicationContext(), Constant.NETWORKTYPE ) )
                .addHeader( "x-agent", VersionUtil.getVersionCode( getApplicationContext() ) )
                .addHeader( "x-platform", Constant.ANDROID )
                .addHeader( "x-devtype", Constant.IMEI )
                .addHeader( "x-token", ParamUtil.GroupMap( getApplicationContext(), "" ) )
                .enqueue( new JsonResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        super.onSuccess( statusCode, response );
                        try {
                            JSONObject jsonObject = new JSONObject( response.toString() );
                            if (jsonObject.getInt( "status" ) >= 0) {
                                ShopBasicBean bean = GsonUtil.GsonToBean( response.toString(), ShopBasicBean.class );
                                if (bean == null) return;
                                ShopBasicBean.ShopBasicData result = bean.getResult();
                                JumpToShopDetailUtil.start2ActivityOfHeadBean( getApplicationContext(), result );
                            } else {
                                String result = jsonObject.getString( "result" );
                                ToastUtils.showToast( getApplicationContext(), result );
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        ToastUtils.showToast( getApplicationContext(), Constant.NONET );
                    }
                } );
    }

    private void customShareStyle() {
        NiceDialog.init().setLayoutId( R.layout.save_money_dialog )
                .setConvertListener( new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        holder.setOnClickListener( R.id.tv_cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        } );
                        LinearLayout wcaht_friend = holder.getView( R.id.wcaht_friend );
                        LinearLayout wchat_circle = holder.getView( R.id.wchat_circle );
                        LinearLayout qq_friend = holder.getView( R.id.qq_friend );
                        LinearLayout save_img = holder.getView( R.id.save_img );
                        wcaht_friend.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                wchatFriendShare();
                            }
                        } );
                        wchat_circle.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                wchatFriendCircle();
                            }
                        } );
                        qq_friend.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                qqFriend();
                            }
                        } );
                        save_img.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                qqZone();
                            }
                        } );
                    }
                } )
                .setShowBottom( true )
                .setOutCancel( true )
                .setAnimStyle( R.style.EnterExitAnimation )
                .show( getSupportFragmentManager() );
    }

    /*微信好友*/
    private void wchatFriendShare() {
        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setTitle( share_title );
        sp.setText( share_content );
        sp.setImageUrl( share_img );
        sp.setUrl( share_url );
        sp.setShareType( Platform.SHARE_WEBPAGE );
        Platform wechat = ShareSDK.getPlatform( Wechat.NAME );
        wechat.setPlatformActionListener( new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.i( "微信好友分享回调", i + "  " + throwable );
            }

            @Override
            public void onCancel(Platform platform, int i) {
            }
        } );
        wechat.share( sp );
    }

    /*微信朋友圈*/
    private void wchatFriendCircle() {
        WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
        sp.setTitle( share_title );
        sp.setText( share_content );
        sp.setImageUrl( share_img );
        sp.setUrl( share_url );
        sp.setShareType( Platform.SHARE_WEBPAGE );
        Platform weChat = ShareSDK.getPlatform( WechatMoments.NAME );
        weChat.setPlatformActionListener( new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
            }

            @Override
            public void onCancel(Platform platform, int i) {
            }
        } );
        weChat.share( sp );
    }

    /*qq好友*/
    private void qqFriend() {
        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setTitle( share_title );
        sp.setText( share_content );
        sp.setImageUrl( share_img );
        sp.setTitleUrl( share_url );
        sp.setShareType( Platform.SHARE_WEBPAGE );
        Platform qq = ShareSDK.getPlatform( QQ.NAME );
        if (!qq.isClientValid()) {
            //客户端不可用，
            return;
        }
        qq.setPlatformActionListener( new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
            }

            @Override
            public void onCancel(Platform platform, int i) {
            }
        } );
        qq.share( sp );
    }

    /*qq空间*/
    private void qqZone() {
        QZone.ShareParams sp = new QZone.ShareParams();
        sp.setTitle( share_title );
        sp.setText( share_content );
        sp.setImageUrl( share_img );
        sp.setTitleUrl( share_url );
        Platform qq = ShareSDK.getPlatform( QZone.NAME );
        if (!qq.isClientValid()) {
            return;
        }
        qq.setPlatformActionListener( new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
            }

            @Override
            public void onCancel(Platform platform, int i) {
            }
        } );
        qq.share( sp );
    }

    /*保存图片至相册*/
    private void saveImgToLocal(String img) {
        new Thread( new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;
                try {
                    imageurl = new URL( img );
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
                    conn.setDoInput( true );
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream( is );
                    is.close();
                    Message msg = new Message();
                    // 把bm存入消息中,发送到主线程
                    msg.obj = bitmap;
                    handler.sendMessage( msg );
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } ).start();
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Bitmap bitmap = (Bitmap) msg.obj;
            CommonUtil.saveBitmap2file( bitmap, getApplicationContext() );
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown( keyCode, event );
    }

    @Override
    protected void onDestroy() {
        if (webview != null) {
            webview.loadDataWithBaseURL( null, "", "text/html", "utf-8", null );
            webview.clearHistory();
            ((ViewGroup) webview.getParent()).removeView( webview );
            webview.destroy();
            webview = null;
        }
        super.onDestroy();
    }
}
